use crate::user::models;
use diesel::r2d2::{self, ConnectionManager};
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

pub fn first_usr()-> Result<Option<models::User>, diesel::result::Error>{
    dotenv().ok();

    let connspec = env::var("DATABASE_URL").expect("DATABASE_URL");
    println!("{}",connspec);
    let manager = ConnectionManager::<SqliteConnection>::new(connspec);
    let pool = r2d2::Pool::builder()
        .build(manager)
        .expect("Failed to create pool.");
    let conn = pool.get().expect("couldn't get db connection from pool");
    use crate::user::schema::users::dsl::*;

    let user = users
        .first::<models::User>(&conn)
        // .expect("no");
        .optional()?;//may be none
    // println!("{}",user.name);
    Ok(user)
}
pub fn main() {
    let user=first_usr();
    match user {
        Ok(v) => {
            println!("ok");
            match v {
                None => {println!("none");},
                Some(quotient) => {
                    println!("{}", quotient.name)
                },
            }
        }
        ,
        Err(_) => {},
    }
}
